#include <54func.h>

int mysqlInit(MYSQL **pmysqlConn){
    char * host = "192.168.0.69";
    char * user = "root";
    char * passwd = "1234";
    char * db = "54test";

    *pmysqlConn = mysql_init(NULL);
    if(mysql_real_connect(*pmysqlConn, host, user, passwd, db, 0, NULL, 0) == NULL){
        printf("error:%s\n", mysql_error(*pmysqlConn));
        return EXIT_FAILURE;
    }
    printf("server was connected mysql!\n");
    return 0;
}

int mysqlQuery(MYSQL *pmysqlConn, const char* userName){
    //1、执行查询
    char query[1024] = "select * from user where username = '";
    sprintf(query, "%s%s%s", query, userName, "';");
    printf("query = %s\n", query);
    int ret = mysql_query(pmysqlConn, query);
    if(0 != ret){
        printf("error query1: %s\n", mysql_error(pmysqlConn));
        return EXIT_FAILURE;
    }
    
    //2、获取结果集，N行数据
    MYSQL_RES * result = mysql_store_result(pmysqlConn);
    if(result == NULL){
        printf("eooer query2: %s\n", mysql_error(pmysqlConn));
        return EXIT_FAILURE;
    }
    int rows = mysql_num_rows(result);
    int cols = mysql_num_fields(result);
    printf("rows = %d, cols = %d\n", rows, cols);

    //3、获取每一行的数据
    MYSQL_ROW row;
    while((row = mysql_fetch_row(result)) != NULL){
        for(unsigned int i = 0; i < cols; ++i){
            printf("%10s ", row[i]);
        }
        printf("\n");
    }

    //4、释放结果集
    mysql_free_result(result);
    mysql_close(pmysqlConn);
    return 0;
}

int generateInt(int length, char str[]){
    int flag, i;
    char* string;
    srand((unsigned) time(NULL));
    if((string = (char*) malloc(length)) == NULL){
        printf("malloc failed!flag:14\n");
        return 0;
    }
    for(i = 0; i < length; i++){
        flag = rand() % 3;
        switch (flag) {
            case 0: string[i] = '0' + rand() % 10; break;
            default: string[i] = '1'; break;
        }
    }
    string[length] = '\0';
    memcpy(str, string, length);
    return 0;
}

int generateStr(int length, char str[]){
    int flag, i;
    char* string;
    srand((unsigned) time(NULL ));
    if ((string = (char*) malloc(length)) == NULL ) {
        printf("malloc failed!flag:14\n");
        return 0;
    }
    for (i = 0; i < length+1; i++) {
    flag = rand() % 3;
    switch (flag) {
    case 0: string[i] = 'A' + rand() % 26; break;
    case 1: string[i] = 'a' + rand() % 26; break;
    case 2: string[i] = '0' + rand() % 10; break;
    default: string[i] = 'x'; break;
    }
    }
    string[length] = '\0';
    memcpy(str, string, length);
    return 0;
}

int generateSalt(char salt[]){
    char str1[2];
    generateInt(1, str1);
    printf("str1 = %s\n", str1);
    char str2[2];
    generateInt(1, str2);
    printf("str2 = %s\n", str2);
    char str3[10];
    generateStr(7, str3);
    printf("str3 = %s\n", str3);
    bzero(salt, strlen(salt));
    sprintf(salt, "%s%s%s%s%s%s", "$", str1, "$", str2, str3, "$");
    printf("salt = %s\n", salt);
    return 0;
}

typedef struct userTable_s{
    int id;//自动增长
    char userName[20];//用户名
    char userSalt[20];//用户盐值
    char userCryptpasswd[100];//用户密文
    char userPwd[1000];//用户路径(根目录下)
}userTable_t;
typedef struct fileTable_s{
    int id;//自动增长
    char fileName[100];//文件名, 来自客户端
    char userName[20];//用户名
    int pre_id;//该文件上一层目录id
    char path[100];//路径
    char type[2];//d-目录文件, f-普通文件
    char *fileMd5;//目录文件md5码=NULL, 文件有md5码
    int tomb;//存在=0, 不存在=1
}fileTable_t;
int passwdVerify(MYSQL *pmysqlConn, const char* userName, const char* userPasswd, int *pQueryResult){
    char query[300] = "select * from user where username = '";
    sprintf(query, "%s%s%s", query, userName, "';");
    printf("query = %s\n", query);
    
    int ret = mysql_query(pmysqlConn, query);
    if(0 != ret){
        printf("error query1: %s\n", mysql_error(pmysqlConn));
        return EXIT_FAILURE;
    }
    
    //2、获取结果集，N行数据
    MYSQL_RES * result = mysql_store_result(pmysqlConn);
    if(result == NULL){
        printf("eooer query2: %s\n", mysql_error(pmysqlConn));
        return EXIT_FAILURE;
    }
    int rows = mysql_num_rows(result);
    int cols = mysql_num_fields(result);
    printf("rows = %d, cols = %d\n", rows, cols);
    if(0 == rows){
        //没查到
        printf("未查询到用户: %s\n", userName);
        *pQueryResult = -1;
        return 0;
    }

    //3、获取每一行的数据
    MYSQL_ROW row;
    char salt[20] = {0};
    char mysqlCyrptpasswd[300] = {0};
    if(rows > 0){
        while((row = mysql_fetch_row(result)) != NULL){
            for(unsigned int i = 0; i < cols; ++i){
                printf("%s\t", row[i]);
                // id-0, username-1, salt-2, cyrptpasswd-3, pwd-4
                if(2 == i){
                    memcpy(salt, row[i], strlen(row[i]));
                }
                if(3 == i){
                    memcpy(mysqlCyrptpasswd, row[i], strlen(row[i]));
                }
            }
            printf("\n");
        }
    }
    //验证用户密码
    char *cyrptPasswd = crypt(userPasswd, salt);
    printf("salt = %s\n", salt);
    printf("mysqlCyrptpasswd = %s\n", mysqlCyrptpasswd);
    printf("cyrptPasswd = %s\n", cyrptPasswd);
    if((strcmp(mysqlCyrptpasswd, cyrptPasswd)) == 0){
        //密码正确
        *pQueryResult = 1;
    }
    else{
        *pQueryResult = -1;
    }

    //4、释放结果集
    mysql_free_result(result);
    return 0;
}

int mysqlInsert(MYSQL *pmysqlConn, const char* tableName, void *arg){
    if(strcmp(tableName, "user") == 0){
        userTable_t *puserTable = (userTable_t *)arg;
        char query[1024] = "insert into user(username, salt, cyrptpasswd, pwd) values(";
        sprintf(query, "%s%s%s%s%s", query, "'", puserTable->userName, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", puserTable->userSalt, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", puserTable->userCryptpasswd, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", puserTable->userPwd, "'", ");");
        printf("query = %s\n", query);
        int ret = mysql_query(pmysqlConn, query);
        if(0 != ret){
            printf("插入用户表失败\n");
            printf("error query: %s\n", mysql_error(pmysqlConn));
            return EXIT_FAILURE;
        }
    }
    else if(strcmp(tableName, "file") == 0){
        fileTable_t *pfileTable = (fileTable_t *)arg;
        char query[1024] = "insert into file(filename, user, pre_id, path, type, md5, tomb) values(";
        sprintf(query, "%s%s%s%s%s", query, "'", pfileTable->fileName, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", pfileTable->userName, "'", ", ");
        sprintf(query, "%s%s%d%s%s", query, "'", pfileTable->pre_id, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", pfileTable->path, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", pfileTable->type, "'", ", ");
        sprintf(query, "%s%s%s%s%s", query, "'", pfileTable->fileMd5, "'", ", ");
        sprintf(query, "%s%s%d%s%s", query, "'", pfileTable->tomb, "'", ");");
        printf("query = %s\n", query);
        int ret = mysql_query(pmysqlConn, query);
        if(0 != ret){
            printf("插入文件表失败\n");
            printf("error query: %s\n", mysql_error(pmysqlConn));
            return EXIT_FAILURE;
        }
    }
    else{
        printf("tableName error!\n");
        return -1;
    }
    return 0;
}
typedef struct train_s{
    int length;
    char data[1000];
    int code;//状态码
}train_t;

int parseStr(char beforeParseStr[], char* afterParseStr[]){
    int i = 0;
    afterParseStr[i] = strtok(beforeParseStr, " ");
    printf("afterParseStr[%d] = %s", i, afterParseStr[i]);
    ++i;
    afterParseStr[i] = strtok(NULL, " ");
    while (afterParseStr[i] != NULL)
    {
        afterParseStr[i] = strtok(NULL, " ");
        printf("afterParseStr[%d] = %s\n", i, afterParseStr[i]);
        ++i;
    }
    return 0;
}
int parseStr1(char beforeParseStr[], char* afterParseStr[], int *argcNum){
    *argcNum = 0;
    char* token = NULL;
    token = strtok(beforeParseStr, " ");
    afterParseStr[*argcNum] = token;
    printf("afterParseStr[%d] = %s", *argcNum, afterParseStr[*argcNum]);
    ++(*argcNum);
    token = strtok(NULL, " ");
    while (token != NULL)
    {
        token = strtok(beforeParseStr, " ");
        afterParseStr[*argcNum] = token;
        printf("afterParseStr[%d] = %s\n", *argcNum, afterParseStr[*argcNum]);
        ++(*argcNum);
    }
    return 0;
}
int main(int argc, char **argv){
    // MYSQL *pmysqlConn = NULL;
    // char query[300] = "select * from user where username = '";
    // char username[20] = "test1";
    // sprintf(query, "%s%s%s", query, username, "'");
    // printf("query = %s\n", query);
    // mysqlInit(&pmysqlConn);
    // mysqlQuery(pmysqlConn, query);

    // char *str1 = generateSalt(1);
    // printf("str1 = %s\n", str1);
    // char *str2 = generateSalt(8);
    // printf("str2 = %s\n", str2);
    // char newStr[20];
    // sprintf(newStr, "%s%s%s%s%s", "$", str1, "$", str2, "$");
    // printf("newStr = %s\n", newStr);
    // char salt[30] = {0};
    // char *pwd1, *pwd2, *pwd3;
    // char daolo[30] = "$";
    // pwd1 = generateInt(1);
    // sprintf(daolo, "%s%s", daolo, pwd1);
    // printf("daolo = %s\n", daolo);
    // pwd2 = generateInt(1);
    // printf("pwd2 = %s\n", pwd2);
    // sprintf(daolo, "%s%s%s", daolo, "$", pwd2);
    // printf("daolo = %s\n", daolo);
    // pwd3 = generateSalt(7);
    // sprintf(daolo, "%s%s%s", daolo, pwd3, "$");
    // printf("daolo = %s\n", daolo);
    // char passwd[30] = "1dsg";
    // char *buf = crypt(passwd, "$6$6H8P4H15$");
    // printf("buf = %s\n", buf);

    // MYSQL *pmysqlConn = NULL;
    // mysqlInit(&pmysqlConn);
    // userTable_t puserTable;
    // char userName[20] = "test4";
    // char userPasswd[20] = "4";
    // char userPwd[100];
    // printf("generate salt start!\n");
    // char salt[20];
    // //bzero(salt, strlen(salt));
    // char str1[2];
    // generateInt(1, str1);
    // printf("str1 = %s\n", str1);
    // char str2[2];
    // generateInt(1, str2);
    // printf("str2 = %s\n", str2);
    // char str3[10];
    // generateStr(7, str3);
    // printf("str3 = %s\n", str3);
    // sprintf(salt, "%s%s%s%s%s%s", "$", str1, "$", str2, str3, "$");
    // printf("salt = %s\n", salt);
    //2、生成密文 = crypt(passwd, salt);
    // char salt[20];
    // generateSalt(salt);
    // char *cyrptPasswd = crypt(userPasswd, salt);
    // while(cyrptPasswd == NULL){
    //     // generateInt(1, str1);
    //     // generateInt(1, str2);
    //     // generateStr(7, str3);
    //     generateSalt(salt);
    //     //sprintf(salt, "%s%s%s%s%s%s", "$", str1, "$", str2, str3, "$");
    //     cyrptPasswd = crypt(userPasswd, salt);
    // }
    // printf("salt = %s\n", salt);
    // printf("cyrptPasswd = %s\n", cyrptPasswd);
    // bzero(&puserTable, sizeof(userTable_t));
    // memcpy(puserTable.userName, userName, strlen(userName));
    // memcpy(puserTable.userSalt, salt, strlen(salt));
    // memcpy(puserTable.userCryptpasswd, cyrptPasswd, strlen(cyrptPasswd));
    // printf("puserTable.userName = %s\n", puserTable.userName);
    // printf("puserTable.userSalt = %s\n", puserTable.userSalt);
    // printf("puserTable.userCryptpasswd = %s\n", puserTable.userCryptpasswd);
    // sprintf(userPwd, "%s%s%s", "Netdisk/", userName, "/");
    // printf("userPwd = %s\n", userPwd);
    // memcpy(puserTable.userPwd, userPwd, strlen(userPwd));
    // char ptableName[20] = "user";
    // mysqlInsert(pmysqlConn, ptableName, &puserTable);

    // MYSQL *pmysqlConn = NULL;
    // mysqlInit(&pmysqlConn);
    // char userName[20] = "test1";
    // char userPasswd[20] = "1234";
    // char *pw = userPasswd;
    // int queryResult;
    // passwdVerify(pmysqlConn, userName, userPasswd, &queryResult);
    // printf("queryResult = %d\n",queryResult);
    
    // MYSQL *pmysqlConn = NULL;
    // mysqlInit(&pmysqlConn);
    // fileTable_t fileTable;
    // bzero(&fileTable, sizeof(fileTable_t));
    // char fileTableName[20] = "file";
    // char userName[20] = "test5";
    // memcpy(fileTable.fileName, userName, strlen(userName));
    // memcpy(fileTable.userName, userName, strlen(userName));
    // fileTable.pre_id = -1;//目录文件
    // sprintf(fileTable.path, "%s%s%s", "Netdisk/", userName, "/");
    // memcpy(fileTable.type, "d", 1);
    // fileTable.fileMd5 = NULL;
    // fileTable.tomb = 0;//存在=0, 不存在=1
    // printf("fileTable.fileName = %s\n", fileTable.fileName);
    // printf("fileTable.pre_id = %d\n", fileTable.pre_id);
    // printf("fileTable.path = %s\n", fileTable.path);
    // printf("fileTable.type = %s\n", fileTable.type);
    // printf("fileTable.fileMd5 = %s\n", fileTable.fileMd5);
    // printf("fileTable.tomb = %d\n", fileTable.tomb);
    // mysqlInsert(pmysqlConn, fileTableName, &fileTable);
    
    
    // int sockFd = socket(AF_INET, SOCK_STREAM, 0);
    // int ret = -1;
    // int reuse = 1;
    // ret = setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    // ERROR_CHECK(ret, -1, "setsockopt");
    // struct sockaddr_in addr;
    // addr.sin_family = AF_INET;
    // addr.sin_addr.s_addr = inet_addr(argv[1]);
    // addr.sin_port = htons(atoi(argv[2]));
    // sockFd = socket(AF_INET, SOCK_STREAM, 0);
    // ret = connect(sockFd, (struct sockaddr *)&addr, sizeof(addr));
    // ERROR_CHECK(ret, -1, "connect");
    // printf("connect successful\n");
    // train_t train;
    // bzero(&train, sizeof(train_t));
    // train.code = 4;
    // ret = send(sockFd, &train, sizeof(train_t), MSG_NOSIGNAL);//发送注册申请
    // ERROR_CHECK(ret, -1, "send");
    
    char str[100];
    bzero(&str, 100);
    fgets(str, 100, stdin);
    printf("before str = %s", str);
    char* p[] = {0};
    int argcNum = 0;
    parseStr1(str, p, &argcNum);
    printf("after str = %s\n", str);
    printf("argcNum = %d\n", argcNum);
    for(int i = 0; i < argcNum; ++i){
        printf("p[%d] = %s\n", i, p[i]);
    }
    return 0;
}